<!--奖金测算-机构测算-机构测算方案-->
<template>
  <basic-container class="bonus">
    <div v-if="show">
      <!--搜索栏目-->
      <div class="search-form">
        <el-form :inline="true" :model="searchForm" >
          <el-form-item label="负责机构">
            <el-select v-model="searchForm.bonusItemDeptId" placeholder="负责机构" @change="changeBonusItemDeptId">
              <el-option
                v-for="item in bonusItemDeptIdOptions"
                :key="item.deptId"
                :label="item.name"
                :value="item.deptId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="奖金项">
            <el-input v-model="searchForm.computeName" placeholder="奖金项" clearable></el-input>
          </el-form-item>
          <el-form-item label="奖金编码">
            <el-input v-model="searchForm.eccCode" placeholder="奖金编码" clearable></el-input>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </div>
      </div>
      <div>
        <div class="menu-left">
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="danger" plain @click="del(true)">删除</el-button>
          <el-button type="primary" plain @click="schemeCompare">方案对比</el-button>
          <el-button type="primary" plain @click="copyScheme">复制</el-button>
        </div>
        <el-table :data="tableData" v-loading="loading" border
                  @selection-change="handleSelectionChange"
                  :row-class-name="iconHadler"
                  :height="`calc(100vh - 280px )`">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-table :data="props.row.children">
                <el-table-column
                width="60px"
                prop=""
                label="序号"
                align="center"
              >
                <template slot-scope="scope">
                  {{scope.$index + 1}}
                </template>
              </el-table-column>
                <el-table-column
                  prop="bonusItemName"
                  label="子奖金项"
                  align="left"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                  prop="bonusItemCode"
                  label="奖金编号(内)"
                  align="center"
                ></el-table-column>
                <el-table-column
                  prop="computeDate"
                  label="奖金项时间"
                  align="center"
                  width="220"
                >
                </el-table-column>
                <el-table-column
                  prop="createDeptName"
                  label="负责机构/部门（二级）"
                  width="180"
                  align="center"
                ></el-table-column>
                <el-table-column
                  prop="bonusCount"
                  label="可发金额"
                  align="center"
                ></el-table-column>
                <el-table-column
                  prop="computeStatus"
                  label="状态"
                  align="center"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column label="操作" width="200px" align="center">
                  <template slot-scope="{row}">
                    <el-button type="text" @click="configComputeCol(row)">配置</el-button>
                    <el-button type="text" @click="measurementHandle(row)" :disabled="!!!row.viewColumn">测算</el-button>
                    <el-button type="text" class="delete_btn" @click="del(false,row)">删除</el-button>
                  </template>
                </el-table-column>

              </el-table>
            </template>
          </el-table-column>
         <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            width="60px"
            prop=""
            label="序号"
            align="center"
          >
            <template slot-scope="scope">
              {{(page.currentPage - 1) * page.pageSize + scope.$index + 1}}
            </template>
          </el-table-column>
          <el-table-column
            prop="computeName"
            label="方案名称"
            min-width="220"
            align="left"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="bonusItemName"
            label="奖金项"
            align="left"
            min-width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="bonusItemEccCode"
            label="ECC奖金编号"
            min-width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="bonusItemCode"
            label="系统内奖金编号"
            min-width="150"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="computeDate"
            label="奖金发放时间"
            align="center"
            width="140"
          >
          </el-table-column>
          <el-table-column
            prop="deptName"
            label="负责机构"
            min-width="130"
            show-overflow-tooltip
            align="center"
          ></el-table-column>
          <el-table-column
            prop="bonusCount"
            label="总金额"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="computeStatus"
            label="状态"
            align="center"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作" width="230px" align="center" fixed="right">
            <template slot-scope="{row}">
              <el-button type="text" @click="edit(row)">编辑</el-button>
<!--              <el-button type="text" :disabled="!row.children || row.children.length==0" @click="amountDivide(row)">分配</el-button>-->
              <el-button type="text" @click="configComputeCol(row)">配置</el-button>
              <el-button type="text" @click="measurementHandle(row)" :disabled="!!!row.viewColumn">测算</el-button>
              <el-button type="text" class="delete_btn" @click="del(false,row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="page.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page.pageSize"
            :total="page.total"
            background
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <!--方案对比-->
    <schemeCompare ref="schemeCompareRef" v-if="showScheme" @close="close"></schemeCompare>
    <!-- 复制方案-->
    <copy-scheme ref="copyScheme" @refresh="refresh"></copy-scheme>
    <!--测算-->
    <measurement ref="measurementRef" v-if="showMeasurement" @close="close" @refresh="refresh"></measurement>
    <!--新增方案-->
    <programmeForm ref="programmeFormRef" :bonusItemDeptIdOptions="bonusItemDeptIdOptions" :bonusItemsScopeOptions="bonusItemsScopeOptions" @refresh="refresh"></programmeForm>
     <!--金额分配 -->
     <amountDivideForm ref="amountDivideFormRef" @refresh="refresh"></amountDivideForm>
     <!--参与测算项配置-->
     <config-compute-item-form ref="configComputeItemFormRef" @refresh="refresh"></config-compute-item-form>
  </basic-container>
</template>

<script>
import programmeForm from "@/views/bonusmanage/calculate/user/programme/programmeForm";
import schemeCompare from '@/views/bonusmanage/calculate/user/programme/schemeCompare'
import measurement from '@/views/bonusmanage/calculate/user/programme/measurement'
import amountDivideForm from "@/views/bonusmanage/calculate/user/programme/amountDivideForm";
import { fetchList, deleteBatch} from '@/api/bonus/calculateuserprogramme'
import {getOrg} from '@/api/bonus/items'
import configComputeItemForm from "@/views/bonusmanage/calculate/user/programme/configComputeItemForm";
import CopyScheme from "@/views/bonusmanage/components/copyScheme";
export default{
  name:"items",
  components:{
    programmeForm,
    schemeCompare,
    measurement,
    amountDivideForm,
    configComputeItemForm,
    CopyScheme
  },
  data() {
    return {
      show:true ,
      showScheme:false,
      showMeasurement:false,
      loading:false,
      page:{
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      searchForm: {bonusItemDeptId:''},
      bonusItemDeptIdOptions:[],
      bonusItemsScopeOptions:[
        {label:'公共',value:'公共'},
        {label:'私有',value:'私有'}
      ], //奖金范围
      multipleSelection: [],
      ids:[]
    }
  },
  computed: {

  },
  created(){
    //初始化获取二级机构
    this.getOrg() ;
  },
  methods:{
    getOrg(){
      getOrg().then(res => {
        this.bonusItemDeptIdOptions = res.data.data
        this.$set(this.searchForm,'bonusItemDeptId',this.bonusItemDeptIdOptions[0].deptId)
        //获取数据
        this.onLoad() ;
      })
    },
    handleSearch(){
      this.onLoad() ;
    },
    changeBonusItemDeptId(){
      this.onLoad() ;
    },
    onLoad(){
      this.loading = true ;
      fetchList(Object.assign({
        current: this.page.currentPage,
        size: this.page.pageSize
      },this.searchForm)).then(res=>{
        if (res.data.code == 0){
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total ;
          this.page.currentPage = res.data.data.current ;
        }else{
          this.$message.error(res.data.msg)
        }
        this.loading = false ;
      }).catch(()=>{
        this.loading = false ;
      })
    },
    handleSelectionChange(list){
      this.multipleSelection = list ;
      this.ids = this.multipleSelection.map((ele) => {
        return ele.id
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.currentPage = 1;
      this.onLoad() ;
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val;
      this.onLoad() ;
    },
    add(){
      this.$refs.programmeFormRef.init('add',{},this.searchForm.bonusItemDeptId) //负责机构的id传过去
    },
    edit(item){
      this.$refs.programmeFormRef.init('edit',JSON.parse(JSON.stringify(item)),this.searchForm.bonusItemDeptId)//负责机构的id传过去
    },
    del(batch,item){
      let msg = '' ;
      let ids = ''
      if (batch){ //批量
        msg = '确定删除选中的数据吗?'
        if (this.multipleSelection.length == 0){
          this.$message.error('请勾选要删除的数据')
          return ;
        }
        ids = this.ids.toString() ;
      }else{ //单条
        msg = '确定删除此条数据吗?'
        ids = item.id ;
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true ;
        deleteBatch({ids:ids}).then(res=>{
          this.$message.success('操作成功')
          this.onLoad() ;
          this.loading = false ;
        }).finally(()=>{
          this.loading = false ;
        })
      }).catch(() => {

      });
    },
    schemeCompare(){
       this.show = false ;
       this.showScheme = true ;
       this.showMeasurement = false ;
       this.$nextTick(()=>{
         this.$refs.schemeCompareRef.init(this.ids);
       })
    },
    //复制方案
    copyScheme(){
      if (this.multipleSelection.length != 1 ){
        this.$message.error("请只选择一条进行复制")
        return
      }

      this.$refs.copyScheme.init('ry',this.multipleSelection) ;

    },
    //测算
    measurementHandle(item){
      this.show = false ;
      this.showScheme = false ;
      this.showMeasurement = true ;
      this.$nextTick(()=>{
        this.$refs.measurementRef.init(JSON.parse(JSON.stringify(item))) ;
      })
    },
    //配置
    configComputeCol(item){
      this.$nextTick(()=>{
        this.$refs.configComputeItemFormRef.init(item) ;
      })
    },
    refresh(){
      this.onLoad() ;
    },
    close(refresh){
      this.show = true ;
      this.showScheme = false ;
      this.showMeasurement = false ;
      if (refresh){
        this.refresh() ;
      }
    },
    amountDivide(row){
      this.$refs.amountDivideFormRef.init('add',JSON.parse(JSON.stringify(row))) ;
    },
    iconHadler({row}) {
      if(!row.children || row.children.length==0){
        return 'icon-no';
      }
    },
  }
}
</script>
<style scoped>
/*如果测算方案无下级，去掉箭头*/
::v-deep .icon-no .el-table__expand-icon {
  display: none;
}
</style>
